#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
char buf[1 << 20], *p1 = buf, *p2 = buf;
#define getchar() ((p1 == p2 && ((p2 = (p1 = buf) + fread(buf, 1, 1 << 20, stdin)), p1 == p2)) ? EOF : (*p1++))
template<typename T> void read(T& x)
{
    x = 0;
    int k = 1;
    char c = getchar();
    for (; !isdigit(c); c = getchar()) if (c == '-') k = -k;
    for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
    x *= k;
}
constexpr int maxn = 200000 + 10;
int a[maxn], h[maxn];
i64 t[maxn << 2];
i64 query(int o, int l, int r, int k)
{
    if (l == r) return t[o];
    int m = (l + r) >> 1;
    if (k <= m) return query(o << 1, l, m, k);
    else return query(o << 1 | 1, m + 1, r, k);
}
void update(int o, int l, int r, int k, i64 x)
{
    if (l == r) return t[o] = max(t[o], x), void();
    int m = (l + r) >> 1;
    if (k <= m) update(o << 1, l, m, k, x);
    else update(o << 1 | 1, m + 1, r, k, x);
    t[o] = max(t[o << 1], t[o << 1 | 1]);
}
void solve()
{
    int n;
    read(n);
    for (int i=1;i<=n;++i) read(a[i]), h[i] = a[i];
    sort(h + 1, h + n + 1);
    int m = unique(h + 1, h + n + 1) - h - 1;
    for (int i=1;i<=n;++i) a[i] = lower_bound(h + 1, h + m + 1, a[i]) - h;
    a[n + 1] = 0;
    memset(t, 0xcf, sizeof(t));
    i64 tag = 0;
    update(1, 0, m, 0, 0);
    for (int i=2;i<=n+1;++i)
    {
        i64 mx = max(t[1], query(1, 0, m, a[i]) + h[a[i]]) + tag;
        if (a[i] == a[i - 1]) tag += h[a[i]];
        update(1, 0, m, a[i - 1], mx - tag);
    }
    printf("%lld\n", query(1, 0, m, a[n]) + tag);
}
int main()
{
    freopen("color.in", "r", stdin);
    freopen("color.out", "w", stdout);
    int t;
    read(t);
    while (t--) solve();
    return 0;
}